package com.onpositive.realmview.queryeditor;

import java.util.Iterator;

import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.util.LocalSelectionTransfer;
import org.eclipse.jface.viewers.ColumnViewer;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.dnd.DragSourceEvent;
import org.eclipse.swt.dnd.DragSourceListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.graphics.TextLayout;
import org.eclipse.swt.graphics.TextStyle;
import org.eclipse.swt.widgets.Display;

import com.onpositive.semantic.model.ui.property.editors.structured.UniversalLabelProvider;
import com.onpositive.viewer.extension.coloring.StyledString;

public final class DragSourceAdapter implements DragSourceListener {
	/**
	 * 
	 */
	private final ColumnViewer owner;
	private Image result;

	/**
	 * @param explorerView
	 */
	public DragSourceAdapter(ColumnViewer explorerView) {
		owner = explorerView;
	}

	public void dragFinished(DragSourceEvent event) {
		if (result != null) {
			result.dispose();
			result = null;
		}

	}

	public void dragSetData(DragSourceEvent event) {
		ISelection selection = owner.getSelection();
		event.data = selection;
	}

	public void dragStart(DragSourceEvent event) {
		StructuredSelection selection = (StructuredSelection) owner
				.getSelection();
		LocalSelectionTransfer.getTransfer().setSelection(selection);

		event.data = selection;
		owner.cancelEditing();
		for (Object obj : selection.toList()) {
			if (selection.size() == 1) {
				Image image = ((UniversalLabelProvider) owner
						.getLabelProvider()).getImage(obj);
				StyledString richTextLabel = ((UniversalLabelProvider) owner
						.getLabelProvider()).getRichTextLabel(obj);
				TextLayout l = new TextLayout(Display.getCurrent());
				Iterator ranges = richTextLabel.getRanges();
				boolean isSelected = false;
				Display display = Display.getCurrent();
				while (ranges.hasNext()) {
					final StyledString.StyleRange curr = (StyledString.StyleRange) ranges
							.next();
					final StyledString.Style style = curr.style;
					if (style != null) {
						final String foregroundColorName = style
								.getForegroundColorName();
						final String backgroundColorName = style
								.getBackgroundColorName();
						if ((foregroundColorName != null)
								|| (backgroundColorName != null)
								|| style.isStrikeout() || style.isUnderline()) {
							final Color foreground = !isSelected
									&& (foregroundColorName != null) ? JFaceResources
									.getColorRegistry()
									.get(foregroundColorName) : null;
							final Color background = !isSelected
									&& (backgroundColorName != null) ? JFaceResources
									.getColorRegistry()
									.get(backgroundColorName) : null;

							final TextStyle textStyle = new TextStyle(null,
									foreground, background);
							textStyle.underline = style.isUnderline();
							textStyle.strikeout = style.isStrikeout();

							l.setStyle(textStyle, curr.offset, curr.offset
									+ curr.length - 1);
						}
					}
				}
				l.setText(richTextLabel.toString());
				Rectangle bounds = l.getBounds();
				Rectangle bounds2 = image.getBounds();
				Rectangle c = new Rectangle(0, 0, bounds2.width + bounds.width,
						Math.max(bounds2.height, bounds.height));
				result = new Image(Display.getCurrent(), c.width, c.height);
				GC gc = new GC(result);
				gc.drawImage(image, 0, 0);
				l.draw(gc, bounds2.width, 2);
				l.dispose();
				gc.dispose();
				event.image = result;

			}
			return;
		}
		event.doit = false;
	}
}